//
//  MainTabBarController.m
//  AAIM
//
//  Created by awh on 2019/11/24.
//  Copyright © 2019年 AAIM. All rights reserved.
//

#import "MainTabBarController.h"
#import "ConversationsViewController.h"
#import "ContactsViewController.h"
#import "DiscoverViewController.h"
#import "MyViewController.h"
#import "MainNC.h"
#import "UITabBar+CustomBadge.h"
#import "ContactsViewController.h"
#import "VDGifPlayerTool.h"
#import "QuotesViewController.h"

@interface MainTabBarController ()<UITabBarControllerDelegate,IMSystemManagerDelegate>
{
    VDGifPlayerTool *addGifView;//加载gif名
}
@property (nonatomic,assign) NSInteger  indexFlag;
@property (nonatomic,strong) NSMutableArray * VCS;//tabbar root VC
@property(nonatomic, strong) ConversationsViewController *conversationsVC;
@property(nonatomic, strong) ContactsViewController *contactorsVC;
@property(nonatomic, strong) DiscoverViewController *discoverHomeVC;
@property(nonatomic, strong) MyViewController *usercenterHomeVC;
@property(nonatomic,strong) QuotesViewController * quotesVC;
@end

@implementation MainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    [[LiftIMClient sharedClient].systemManager addDelegate:self];
    [self setupAllChildViewControllers];
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
}
- (void)setupAllChildViewControllers
{
    _VCS = @[].mutableCopy;
    
    // 1.会话
    self.conversationsVC = [[ConversationsViewController alloc] init];
    [self setupChildViewController:self.conversationsVC title:@"会话" imageName:@"会话-未点击" selectedImageName:@"会话"];
    
    // 2.联系人
    self.contactorsVC = [[ContactsViewController alloc] init];
    [self setupChildViewController:self.contactorsVC title:@"联系人" imageName:@"联系人-未点击" selectedImageName:@"联系人"];
    
    self.quotesVC = [[QuotesViewController alloc] init];
       [self setupChildViewController:self.quotesVC title:@"行情" imageName:@"公众号-未选中状态" selectedImageName:@"公众号-选中状态"];
   
    self.discoverHomeVC = [[DiscoverViewController alloc] init];
    [self setupChildViewController:self.discoverHomeVC title:@"看看" imageName:@"发现-未点击" selectedImageName:@"发现111"];

     
    self.usercenterHomeVC=[[MyViewController alloc]init];
    [self setupChildViewController:self.usercenterHomeVC title:@"我的" imageName:@"我的-未点击" selectedImageName:@"我的"];

    self.viewControllers = _VCS;
    [[UITabBar appearance] setTranslucent:NO];
    UIView * view =[[UIView alloc]init];
    //    view.backgroundColor=KHEXCOLOR(AllBackColor);
    view.frame=self.tabBar.bounds;
    //    [[UITabBar appearance] insertSubview:view atIndex:0];
}

- (void)setupChildViewController:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName {
    
    childVc.title = title;
    childVc.tabBarItem.image = [UIImage imageNamed:imageName];
    UIImage *selectedImage = [UIImage imageNamed:selectedImageName] ;
    childVc.tabBarItem.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //未选中字体颜色
    [childVc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:KHEXCOLOR(kBlackColor),NSFontAttributeName:[UIFont systemFontOfSize:20*SCALAE]} forState:UIControlStateNormal];
    
    //选中字体颜色
    [childVc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:KHEXCOLOR(kMainColor),NSFontAttributeName:[UIFont systemFontOfSize:20*SCALAE]} forState:UIControlStateSelected];
        // 配置导航栏属性
    [GKConfigure setupCustomConfigure:^(GKNavigationBarConfigure *configure) {
        configure.gk_navItemRightSpace = 12.0f;
        configure.backgroundColor=[UIColor whiteColor];
        configure.backStyle = GKNavigationBarBackStyleBlack;
    }];
    UINavigationController *nav =[UINavigationController rootVC:childVc translationScale:NO];
    nav.gk_openScrollLeftPush = YES;
    
    [_VCS addObject:nav];
}


- (UIImageView *)getTabBarButtonImageViewWithCurrentVc:(UIViewController *)currentView{
    UIControl *tabBarButton = [currentView.tabBarItem valueForKey:@"view"];
    if (tabBarButton) {
        UIImageView * tabBarImageView = [tabBarButton valueForKey:@"imageView"];
        if (tabBarImageView) {
            return tabBarImageView;
        }
    }
    return nil;
}


- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    NSString *gifName = @"";
    if (self.selectedIndex == 0) {
        gifName = @"会话";
    }
    else if (self.selectedIndex == 1) {
        gifName = @"联系人";
    }
    else if (self.selectedIndex == 2) {
        gifName = @"发现111";
    }
    else if (self.selectedIndex == 3) {
        gifName = @"我的";
    }
    if (gifName.length>0) {
        VDGifPlayerTool *addGif =  [[VDGifPlayerTool alloc]init];
        if (addGifView) {
            [addGifView remove];
        }

        [addGif startAnimateGifMethod:gifName toView:[self getTabBarButtonImageViewWithCurrentVc:viewController]];
        addGifView = addGif;
    }
}

-(void)setRedDotWithIndex:(NSInteger)index ValueInteger:(NSInteger)integerValue isShow:(BOOL)isShow
{
    if (isShow) {
        [self.tabBar setBadgeStyle:kCustomBadgeStyleNumber value:integerValue atIndex:index];
    }else{
        [self.tabBar setBadgeStyle:kCustomBadgeStyleNone value:integerValue atIndex:index];
    }
}
- (BOOL)shouldAutorotate {
    return [self.selectedViewController shouldAutorotate];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return [self.selectedViewController supportedInterfaceOrientations];
}

-(void)itemMiddleTap{
    if (addGifView) {
        [addGifView remove];
        addGifView = false;
    }
    self.selectedIndex = 2;
}

#pragma mark - IMSystemManagerDelegate
- (void)forcedToLogout {
    [[LiftIMClient sharedClient] closeConnect];
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"温馨提示"
                                            message:@"您的账户在另外一台设备登录,请重新登录"
                                            preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
        [[LoginManager shareLoginManager] loginOut];
    }];
    [alert addAction:okAction];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)socketReconnectTimeout {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.labelText = @"重连超时，请检查您的网络";
    hud.removeFromSuperViewOnHide = YES;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [hud hide:YES];
        [[LoginManager shareLoginManager] loginOut];
        [[LiftIMClient sharedClient] closeConnect];
    });
}
- (void)dealloc {
    [[LiftIMClient sharedClient].systemManager removeDelegate:self];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
